/**
 * 全球遮罩
 * @param {Earth} options.earth 渲染容器
 * @param {Earth} options.points 镂空多边形坐标列表
 * 
 */

function DrawGlobalMask(options) {

    this.viewer = options.earth._viewer
    let point = []
    options.points.forEach(item => {
        point = point.concat(item)
    })

    var hole = Cesium.Cartesian3.fromDegreesArray(point); //地球需要高亮突出的边界坐标
    let fillColor = new Cesium.Color(0, 0, 0, 0.6)
    //新建dataSources
    let dataSource = new Cesium.CustomDataSource("inverseShade");
    this.viewer.dataSources.add(dataSource);
    // 绘制2个半球遮罩
    let hemisphere1 = dataSource.entities.add({
        polygon: {
            hierarchy: {
                //左下左上右上右下
                // 对应经纬度[-0,60],[-0,-60],[-180,-60],[-180,60],[-0,60]
                positions: [
                    {
                        x: 3197104.586923899,
                        y: -0.5580000157243585,
                        z: 5500477.1339386385,
                    },
                    {
                        x: 3197104.586923899,
                        y: -0.5580000157243585,
                        z: -5500477.1339386385,
                    },
                    {
                        x: -3197104.5869239476,
                        y: -3.915323898915733,
                        z: -5500477.1339386385,
                    },
                    {
                        x: -3197104.5869239476,
                        y: -3.915323898915733,
                        z: 5500477.1339386385,
                    },
                    {
                        x: 3197104.586923899,
                        y: -0.5580000157243585,
                        z: 5500477.1339386385,
                    },
                ],
            },
            // 填充的颜色，withAlpha透明度
            material: fillColor,
            // 是否被提供的材质填充
            fill: true,
            // 是否显示
            show: true,
            // 顺序,仅当`clampToGround`为true并且支持地形上的折线时才有效。
            zIndex: 10,
        },
    });
    let hemisphere2 = dataSource.entities.add({
        polygon: {
            // 获取指定属性（positions，holes（图形内需要挖空的区域））
            hierarchy: {
                //左下左上右上右下
                // 对应经纬度[0,60],[0,-60],[180,-60],[180,60],[0,60]
                positions: [
                    {
                        x: 3197104.586923899,
                        y: 0.5580000157243585,
                        z: 5500477.1339386385,
                    },
                    {
                        x: 3197104.586923899,
                        y: 0.5580000157243585,
                        z: -5500477.1339386385,
                    },
                    {
                        x: -3197104.5869239476,
                        y: 3.915323898915733,
                        z: -5500477.1339386385,
                    },
                    {
                        x: -3197104.5869239476,
                        y: 3.915323898915733,
                        z: 5500477.1339386385,
                    },
                    {
                        x: 3197104.586923899,
                        y: 0.5580000157243585,
                        z: 5500477.1339386385,
                    },
                ],
                holes: [{ positions: hole }],
            },
            // 填充的颜色，withAlpha透明度
            material: fillColor,
            // 是否被提供的材质填充
            fill: true,
            // 是否显示
            show: true,
            // 顺序,仅当`clampToGround`为true并且支持地形上的折线时才有效。
            zIndex: 10,
        },
    });

    this.dataSource = dataSource
    this.list = [ hemisphere1, hemisphere2 ]
}

DrawGlobalMask.prototype.setShow = function (options) {
    this.list.forEach(item => {
        item.polygon.show = options
    })
}

DrawGlobalMask.prototype.destroy = function () {
    this.viewer.dataSources.remove(this.dataSource)
}


export default DrawGlobalMask